import App from "./App";
import Conn, { ConnectionEvent } from "./network/Conn";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Menu extends cc.Component {

    /** 连接状态 */
    connecting = false
    conn: Conn
    _name: string

    onBtnClick(e, type) {


        if (type == 'local') {
            //单人
            App.startLocal("贪吃蛇")
            cc.director.loadScene('main')
        } else {
            //联机
            if (!this.connecting) {
                this.connecting = true

                let netConn = new Conn()
                netConn.on(ConnectionEvent.joinOk, () => {
                    cc.director.loadScene('main')
                })

                netConn.on(ConnectionEvent.joinErr, (msg: string) => {
                    alert(msg)
                    this.connecting = false
                })
                netConn.start(this._name)
                this.conn = netConn
            }
        }
    }

    onNameChange(e) {
        this._name = e.string
    }
}
